#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include "showdataforgraph.h"
#include "ui_showdataforgraph.h"
#include <QStringList>
#include <QDebug>

showDataForGraph::showDataForGraph(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::showDataForGraph)
{
    ui->setupUi(this);
    timeId = startTimer(10);
}

showDataForGraph::~showDataForGraph()
{
    delete ui;
}

void showDataForGraph::InitSeries(const QStringList &str)
{
    if (initSeries) return;
    int num = 0;
    QStringList serieNames;
    QRegularExpression re("((-\\d.*)|(\\d.*))");
    foreach(QString chilenStr, str){
        QStringList chilenList = chilenStr.split("=");
        QRegularExpressionMatch match = re.match(chilenList.last());
        if(match.hasMatch()){
            serieNames.append(chilenList.first());
            num++;
        }
    }
    serialTotal = num;
    axisX = new QValueAxis;
    axisY = new QValueAxis;
    axisX->setRange(0,maxX);
    axisY->setRange(0,maxY);
    data.resize(num);
    for (int i = 0; i < num; i++) {
        splineSeries.append(new QSplineSeries);
        for (int j = 0; j < maxSize; j++) data[i].append(QPointF(j,0));
    }
    qchart = new QChart;
    QVector<QSplineSeries *>::Iterator iter;
    int index = 0;
    for(iter = splineSeries.begin();iter!=splineSeries.end();iter++){
        (*iter)->setUseOpenGL(true);
        (*iter)->setName(serieNames[index]);
        qchart->addSeries(*iter);
        qchart->setAxisX(axisX, *iter);
        qchart->setAxisY(axisY,*iter);
        index++;
    }
    qchart->setTitle(tr("实时动态曲线"));
    qchart->createDefaultAxes();
    ui->chartView->setChart(qchart);
    initSeries = true;
}

void showDataForGraph::DataReceive(int _value, int index)
{
    int value = _value;
    // 设置坐标轴Y轴的最大范围
    if (value > maxY) {
        maxY = value;
        qchart->axisY()->setRange(minY,maxY);
    }
    if (value < minY) {
        minY = value;
        qchart->axisY()->setRange(minY,maxY);
    }
    data[index].removeFirst();
    data[index].append(QPointF(maxSize ,value));
    QVector<QPointF>::Iterator iter;
    for(iter = data[index].begin();iter != data[index].end();iter++){
        iter->setX(iter->x()-1);
    }
}

void showDataForGraph::ReceiveShowData(const QString &str)
{
    // 删除空格与回车符
    QString str_temp = str;
    str_temp = str_temp.remove(QRegularExpression("\\s"));
    str_temp = str_temp.remove(QRegularExpression("\\n"));

    QStringList strList = str_temp.split(',');
    InitSeries(strList);
    int index = 0;
    QRegularExpression re("((-\\d.*)|(\\d.*))");
    foreach(QString chilenStr, strList){
        QStringList chilenList = chilenStr.split("=");
        QRegularExpressionMatch match = re.match(chilenList.last());
        if(match.hasMatch()){
            if (index >= serialTotal) break;
            DataReceive(match.captured(0).toInt(),index);
            index++;
        }
    }
}

void showDataForGraph::closeEvent(QCloseEvent *event)
{
    emit MeWillClose();
    // emm,劫持了关闭事件居然不会自动调用析构函数？？？？
    this->~showDataForGraph();
}

void showDataForGraph::timerEvent(QTimerEvent *event)
{
   if(event->timerId() == timeId){
       // 暂时关掉图表的自动更新，等数据全更换完成再更新图表，不然容易崩
       ui->chartView->setUpdatesEnabled(false);
       for (int i =0;i < splineSeries.size();i++) {
           splineSeries[i]->replace(data[i]);
       }
       ui->chartView->setUpdatesEnabled(true);
   }
}
